from rest_framework.decorators import action
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from orders.models import OrderInfo
from meiduo_admin.utils import PageNum
from meiduo_admin.serializers.orders import OrdersSerializer


class OrdersView(ReadOnlyModelViewSet):
    queryset = OrderInfo.objects.all()
    permission_classes = [IsAdminUser]
    serializer_class = OrdersSerializer
    pagination_class = PageNum

    def get_queryset(self):
        if self.request.query_params.get('keyword') == '':
            return OrderInfo.objects.all()
        elif self.request.query_params.get('keyword') is None:
            return OrderInfo.objects.all()
        else:
            return OrderInfo.objects.filter(order_id__contains=self.request.query_params.get('keyword'))

    @action(methods=['put'], detail=True)
    def status(self, request, pk):
        try:
            order = OrderInfo.objects.get(order_id=pk)
        except:
            return Response("error: order id")

        status = request.data.get('status')
        if status is None:
            return Response('Miss: satus')

        order.status = status
        order.save()

        return Response(
            {
                'order_id': pk,
                'status': status
            }
        )
